﻿using System;

namespace AirPlanGame
{
    class Program
    {
        //棋盘，定义为全局变量
        private static int[] Maps=new int[100];
        //玩家的姓名
        private static string[] PlayerNames=new string[2];
        //玩家走棋的坐标
        private static int[] PlayerPos=new int[2];

        private static bool[] Flags = new bool[2];
        static void Main(string[] args)
        {
            #region 1.画游戏开头 GameShow()

            GameShow();


            #endregion

            #region 2.输入玩家的姓名 （PlayeName[]）

            //2.输入玩家的姓名 （PlayeName[]）

            Console.WriteLine("请输入玩家A的姓名：");
            PlayerNames[0] = Console.ReadLine();

            //检查用户输入的正确性
            while (string.IsNullOrEmpty(PlayerNames[0]))
            {
                Console.WriteLine("玩家A的姓名不能为空，请重新输入玩家A的姓名：");
                PlayerNames[0] = Console.ReadLine();
            }

            Console.WriteLine("请输入玩家B的姓名：");
            PlayerNames[1] = Console.ReadLine();

            //检查用户输入的正确性

            while (string.IsNullOrEmpty(PlayerNames[1]) || PlayerNames[1] == PlayerNames[0])
            {
                if (string.IsNullOrEmpty(PlayerNames[1]))
                {
                    Console.WriteLine("玩家B的姓名不能为空，请重新输入玩家B的姓名：");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能和玩家A相同，请重新输入玩家B的姓名：");
                    PlayerNames[1] = Console.ReadLine();
                }
            }

            //  打印玩家地图

            Console.Clear();
            GameShow();
            Console.WriteLine("{0}的士兵用A表示",PlayerNames[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
            #endregion

            #region 3.初始化飞行棋地图 InitiaMap()
            //3.初始化飞行棋地图 InitiaMap()

            InitiaMap();

            #endregion

            #region 4.显示地图DrawMap()

            //4.显示地图DrawMap()
            DrawMap();
            #endregion

            #region 5.循环走棋


            //5.循环走棋，只要任意玩家没有走到地图最后一个坐标，每次走棋执行相应的游戏规则 PalyGame()

            //当玩家A和玩家B没有一个人在终点的时候，两个玩家不停去走棋
            while (PlayerPos[0]<99&&PlayerPos[1]<99)
            {
                //棋盘状态，切换玩家状态来轮流走棋
                if (!Flags[0])
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                //判断是否游戏完成
                if (PlayerPos[0] == 99)
                {
                    //玩家A赢了，游戏结束
                    Console.WriteLine("玩家{0}莽撞的赢了玩家{1}", PlayerPos[0], PlayerPos[1]);
                    break;

                }
                //棋盘状态，切换玩家状态来轮流走棋
                if (!Flags[1])
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                //判断是否游戏完成
                if (PlayerPos[1] >= 99)
                {
                    //玩家B赢，游戏结束
                    Console.WriteLine("玩家{0}莽撞的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
                    break;
                }

            }
            #endregion

            #region 6.显示胜利画面
            //6.显示胜利画面
            Win();
            #endregion
            //结束
            Console.ReadKey();
        }


        /// <summary>
        /// 开头画面
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("*************************************************************");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("*************************************************************");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("*************************************************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*************** 软件工程演示项目：飞行棋1.0 *****************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("*************************************************************");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("*************************************************************");
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("*************************************************************"); 
            Console.ForegroundColor = ConsoleColor.White;
        }
        /// <summary>
        /// 初始化棋盘
        /// </summary>
        public static void InitiaMap()
        {
            //幸运轮盘◎
            int[] luckyturn = {2,3,4,16, 23, 40, 55, 68, 83};
            for (var i = 0; i < luckyturn.Length; i++)
            {
                Maps[luckyturn[i]] = 1;//值1表示幸运轮盘◎
            }
            //地雷☆
            int[] mines = { 5, 13, 17, 33, 38, 50,66,88,90,95 };
            for (var i = 0; i < mines.Length; i++)
            {
                Maps[mines[i]] = 2;//值2表示地雷☆
            }
            //暂停▲
            int[] pause = { 9, 10, 15, 36, 45, 56, 84, 91};
            for (var i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;//值3表示暂停▲
            }
            //时空隧道卐
            int[] tunel = {20, 18, 26, 48, 62, 81, 99};
            for (var i = 0; i < tunel.Length; i++)
            {
                Maps[tunel[i]] = 4;//值4表示时空隧道卐
            }
        }
        /// <summary>
        /// 画出地图
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎地雷:☆暂停:▲时空隧道:卐");
            
            //第一横行

            for (var i = 0; i <= 29; i++)
            {

                Console.Write(DrawMapPos(i));
            }
            Console.WriteLine();

            //第一竖行
            for (var i = 30; i < 35; i++)
            {
                for (var j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawMapPos(i));
                
            }


            //第二横行
            for (var i = 64; i > 35; i--)
            {

                Console.Write(DrawMapPos(i));


            }

            //第二竖行

            for (var i = 64; i <= 69; i++)
            {

                Console.WriteLine(DrawMapPos(i));

            }
            //第三横行
            for (var i = 70; i <= 99; i++)
            {

                Console.Write(DrawMapPos(i));


            }
            //结束换行
            Console.WriteLine();


        }
        /// <summary>
        /// 画棋盘的方法，根据坐标显示对应棋盘位置的字符
        /// </summary>
        /// <param name="i">坐标</param>
        /// <returns></returns>
        public static string DrawMapPos(int i)
        {
            string str = "";
            //如果A B重合 显示< >
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if(PlayerPos[0]==i)
            {
                str = "Ａ";
            }
            else if (PlayerPos[1] == i)
            {
                str = "Ｂ";
            }
            else
            {
                //正常位置□ 幸运轮盘:◎地雷:☆暂停:▲时空隧道:卐"
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        str = "卐";
                        break;
                }
                
            }
            return str;
        }
        /// <summary>
        /// 玩游戏，走棋
        /// </summary>
        public static void PlayGame(int player)
        {
            Console.ForegroundColor = ConsoleColor.White;
            //掷骰子
            var r =new Random();
            //生成随机数1-6
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意开始掷骰子",PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0}玩家掷出了{1}",PlayerNames[player],rNumber);
            //走棋,玩家前进
            PlayerPos[player] += rNumber;
            //检测位置是否正确
            ChangePos();

            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动",PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完成",PlayerNames[player]);
            Console.ReadKey(true);

            //按幸运轮盘 地雷 暂停 时空隧道 等等状态判断玩家的行动
            if (PlayerPos[player] == PlayerPos[1 - player])
            {
                //打烧，对方玩家退6格
                Console.WriteLine("显示玩家{0}踩到了玩家{1}，玩家{2}退六格",PlayerNames[player],PlayerNames[1-player],PlayerNames[1-player]);
                PlayerPos[1 - player] -= 6;
                ChangePos();
                Console.ReadKey(true);
            }
            else
            {
                //踩到不同的位置关卡
                switch (Maps[PlayerPos[player]])
                {
                    case 0:
                        //踩到方块 正常
                        Console.WriteLine("玩家{0}踩到方块 行动安全",PlayerNames[player]);
                        Console.ReadKey(true);
                        break;

                    case 1:
                        //踩到幸运轮盘 1.和对方交换位置 2.打烧对方（退六格）
                        Console.WriteLine("玩家{0}踩到幸运轮盘，请选择1--交换位置，2--轰炸对方",PlayerNames[player]);
                        var inputStr = Console.ReadLine();
                        while (true)
                        {
                            if (inputStr=="1")
                            {
                                Console.WriteLine("玩家{0}选择交换位置,玩家{1} {2}互换位置 ", PlayerNames[player], PlayerNames[player], PlayerNames[1 - player]);
                                Console.ReadKey(true);
                                //两个玩家位置交换
                                var n = PlayerPos[1 - player];
                                PlayerPos[1 - player] = PlayerPos[player];
                                PlayerPos[player] = n;
                                Console.WriteLine("玩家{0}和玩家{1}互换位置完成，按任意键继续 ", PlayerNames[player], PlayerNames[player]);
                                Console.ReadKey(true);
                                break;
                            }
                            else if (inputStr=="2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退六格 ", PlayerNames[player], PlayerNames[1 - player],PlayerNames[1 - player]);
                                Console.ReadKey(true);
                                //对方退后六格
                                PlayerPos[1 - player] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}退后了六格,按任意键继续",PlayerNames[1-player]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("玩家{0}踩到幸运轮盘，请选择1--交换位置，2--轰炸对方", PlayerNames[player]);
                                inputStr = Console.ReadLine();
                            }
                        }
                        ChangePos();
                        break;
                    case 2:
                        //踩到地雷 推六格
                        Console.WriteLine("玩家{0}被UFO吸走了,退六格 ",PlayerNames[player]);
                        Console.ReadKey(true);
                        PlayerPos[player] -= 6;
                        ChangePos();
                        break;
                    case 3:
                        //踩到暂停 暂停一回合
                        Console.WriteLine("玩家{0}踩到暂停,暂停一回合 ",PlayerNames[player]);
                        Console.ReadKey(true);
                        Flags[player] = true;
                        break;
                    case 4:
                        //踩到时空隧道 前进十格
                        Console.WriteLine("玩家{0}被黑洞吸走了,前进了10格 ",PlayerNames[player]);
                        PlayerPos[player] += 10;
                        ChangePos();
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();
        }

        /// <summary>
        /// 修正玩家的坐标 不能超出0-99
        /// </summary>
        public static void ChangePos()
        {
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }

            if (PlayerPos[0] > 99)
            {
                PlayerPos[0] = 99;
            }

            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }

            if (PlayerPos[1] > 99)
            {
                PlayerPos[1] = 99;
            }
        }
        public static void Win()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("                                          ◆                      ");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        }
    }


}
